TEST2 This is an R Markdown format used for publishing markdown documents to GitHub. When you click the Knit button all R code chunks are run and a markdown file (.md) suitable for publishing to GitHub is generated.
S-Dot 1시간 측정 평균값이 저장된 csv파일 read
#분석하고자 하는 csv데이터를 sdot이라는 데이터 프레임에 넣기
sdot<-read.csv("data/sdot_20200507.csv", fileEncoding = 'euc-kr', head=TRUE, check.names=FALSE)
#데이터 중 위에서부터 100개의 데이터만 확인
paged_table(sdot) %>% head(100)
sdot에 저장된 값에 대한 데이터 요약
#명령어 summary(데이터프레임)
summary(sdot)
## 시리얼 date hour 초미세먼지 (㎍/㎥)
## V02Q1940043: 24 2020.5.1:20280 Min. : 0.00 Min. : 0.00
## V02Q1940044: 24 1st Qu.: 5.75 1st Qu.: 14.10
## V02Q1940045: 24 Median :11.50 Median : 23.69
## V02Q1940046: 24 Mean :11.50 Mean : 23.56
## V02Q1940047: 24 3rd Qu.:17.25 3rd Qu.: 31.00
## V02Q1940049: 24 Max. :23.00 Max. :175.17
## (Other) :20136
## 미세먼지 (㎍/㎥) 기온 (℃) 상대습도 (%) 풍향 (°)
## Min. : 0.00 Min. :15.24 Min. :31.57 Min. : 0.000
## 1st Qu.: 18.82 1st Qu.:19.46 1st Qu.:57.06 1st Qu.: 0.000
## Median : 31.90 Median :21.21 Median :68.27 Median : 0.000
## Mean : 31.75 Mean :22.02 Mean :64.78 Mean : 2.363
## 3rd Qu.: 41.67 3rd Qu.:24.41 3rd Qu.:72.12 3rd Qu.: 0.000
## Max. :268.33 Max. :34.23 Max. :85.62 Max. :299.593
##
## 풍속 (m/s) 돌풍 풍향 (°) 돌풍 풍속 (m/s) 조도 (lux)
## Min. :0.00000 Min. : 0.000 Min. :0.00000 Min. : 1.00
## 1st Qu.:0.00000 1st Qu.: 0.000 1st Qu.:0.00000 1st Qu.: 5.12
## Median :0.00000 Median : 0.000 Median :0.00000 Median : 1296.71
## Mean :0.01573 Mean : 2.328 Mean :0.03093 Mean : 6664.73
## 3rd Qu.:0.00000 3rd Qu.: 0.000 3rd Qu.:0.00000 3rd Qu.: 9079.19
## Max. :4.55714 Max. :295.750 Max. :8.40714 Max. :50200.48
##
## 자외선 (UVI) 소음 (dB) 진동(x) (g) 진동(y) (g)
## Min. : 1.000 Min. : 0.00 Min. :0.00000 Min. :0.00000
## 1st Qu.: 2.000 1st Qu.:45.38 1st Qu.:0.01833 1st Qu.:0.02815
## Median : 2.130 Median :48.07 Median :0.02276 Median :0.04826
## Mean : 9.105 Mean :48.17 Mean :0.02797 Mean :0.05542
## 3rd Qu.: 10.823 3rd Qu.:50.90 3rd Qu.:0.03500 3rd Qu.:0.07880
## Max. :108.767 Max. :64.52 Max. :0.11077 Max. :0.17696
##
## 진동(z) (g) 진동(x) 최대 (g) 진동(y) 최대 (g) 진동(z) 최대 (g)
## Min. :0.000 Min. :0.00000 Min. :0.0000 Min. : 0.000
## 1st Qu.:1.006 1st Qu.:0.07241 1st Qu.:0.1163 1st Qu.: 1.095
## Median :1.029 Median :0.08708 Median :0.1300 Median : 1.107
## Mean :1.026 Mean :0.09167 Mean :0.1340 Mean : 1.117
## 3rd Qu.:1.050 3rd Qu.:0.10088 3rd Qu.:0.1454 3rd Qu.: 1.124
## Max. :1.116 Max. :2.41815 Max. :2.0540 Max. :10.451
##
## 흑구 온도 (℃) 미세먼지 보정 (㎍/㎥) 초미세먼지 보정 (㎍/㎥)
## Min. : 4.833 Min. :-88.12 Min. :-98.12
## 1st Qu.: 5.000 1st Qu.: 36.09 1st Qu.: 23.23
## Median : 5.000 Median : 46.00 Median : 27.76
## Mean : 7.590 Mean : 43.68 Mean : 26.87
## 3rd Qu.: 5.000 3rd Qu.: 53.31 3rd Qu.: 32.00
## Max. :195.385 Max. :345.60 Max. :179.97
##
위의 값을 확인 했을때 현재 살펴보고자 하는 미세먼지, 초미세먼지 결측값, 음수, 0값을 제거
# 분석 편의상 컬럼 이름 변경
sdot <- sdot %>% dplyr::rename(super_dust =`초미세먼지 보정 (㎍/㎥)`)
sdot <- sdot %>% dplyr::rename(dust =`미세먼지 보정 (㎍/㎥)`)
sdot <- sdot %>% dplyr::rename(model =시리얼)
# Na또는 Nan등 결측값 확인
sum(is.na(sdot))
## [1] 0
0개
#미세먼지 값이 0이하 or 초미세먼지 값이 0 이하 값 제거
sdot <-sdot %>% filter(!super_dust <=0 , !dust <=0)
#명령어 summary(데이터프레임)로 미세먼지 보정, 초미세먼지 보정 값 재확인
summary(sdot)
## model date hour 초미세먼지 (㎍/㎥)
## V02Q1940043: 24 2020.5.1:19960 Min. : 0.00 Min. : 0.03333
## V02Q1940044: 24 1st Qu.: 5.00 1st Qu.: 14.37931
## V02Q1940047: 24 Median :11.00 Median : 23.84413
## V02Q1940050: 24 Mean :11.49 Mean : 23.81125
## V02Q1940052: 24 3rd Qu.:18.00 3rd Qu.: 31.07407
## V02Q1940054: 24 Max. :23.00 Max. :175.16667
## (Other) :19816
## 미세먼지 (㎍/㎥) 기온 (℃) 상대습도 (%) 풍향 (°)
## Min. : 0.06667 Min. :15.24 Min. :31.57 Min. : 0.000
## 1st Qu.: 19.28325 1st Qu.:19.46 1st Qu.:57.10 1st Qu.: 0.000
## Median : 32.08333 Median :21.21 Median :68.29 Median : 0.000
## Mean : 32.08483 Mean :22.02 Mean :64.81 Mean : 2.382
## 3rd Qu.: 41.78571 3rd Qu.:24.41 3rd Qu.:72.13 3rd Qu.: 0.000
## Max. :268.33333 Max. :34.23 Max. :85.62 Max. :299.593
##
## 풍속 (m/s) 돌풍 풍향 (°) 돌풍 풍속 (m/s) 조도 (lux)
## Min. :0.00000 Min. : 0.000 Min. :0.00000 Min. : 1
## 1st Qu.:0.00000 1st Qu.: 0.000 1st Qu.:0.00000 1st Qu.: 5
## Median :0.00000 Median : 0.000 Median :0.00000 Median : 1286
## Mean :0.01595 Mean : 2.349 Mean :0.03131 Mean : 6672
## 3rd Qu.:0.00000 3rd Qu.: 0.000 3rd Qu.:0.00000 3rd Qu.: 9088
## Max. :4.55714 Max. :295.750 Max. :8.40714 Max. :50200
##
## 자외선 (UVI) 소음 (dB) 진동(x) (g) 진동(y) (g)
## Min. : 1.000 Min. : 0.00 Min. :0.00000 Min. :0.00000
## 1st Qu.: 2.000 1st Qu.:45.39 1st Qu.:0.01833 1st Qu.:0.02800
## Median : 2.120 Median :48.04 Median :0.02267 Median :0.04821
## Mean : 9.097 Mean :48.15 Mean :0.02786 Mean :0.05543
## 3rd Qu.: 10.828 3rd Qu.:50.88 3rd Qu.:0.03467 3rd Qu.:0.07900
## Max. :108.767 Max. :64.52 Max. :0.11077 Max. :0.17696
##
## 진동(z) (g) 진동(x) 최대 (g) 진동(y) 최대 (g) 진동(z) 최대 (g)
## Min. :0.000 Min. :0.00000 Min. :0.0000 Min. : 0.000
## 1st Qu.:1.006 1st Qu.:0.07241 1st Qu.:0.1163 1st Qu.: 1.095
## Median :1.029 Median :0.08700 Median :0.1300 Median : 1.107
## Mean :1.026 Mean :0.09149 Mean :0.1340 Mean : 1.117
## 3rd Qu.:1.050 3rd Qu.:0.10067 3rd Qu.:0.1453 3rd Qu.: 1.124
## Max. :1.116 Max. :2.41815 Max. :2.0540 Max. :10.451
##
## 흑구 온도 (℃) dust super_dust
## Min. : 4.833 Min. : 0.1333 Min. : 0.03448
## 1st Qu.: 5.000 1st Qu.: 36.8667 1st Qu.: 23.48148
## Median : 5.000 Median : 46.2069 Median : 27.87750
## Mean : 7.627 Mean : 44.3847 Mean : 27.40362
## 3rd Qu.: 5.000 3rd Qu.: 53.4511 3rd Qu.: 32.07692
## Max. :195.385 Max. :345.6000 Max. :179.96667
##
#결측값 및 0이하 값 제거 확인
#시간에 따른 미세먼지 값 확인
#동적 그래프 생성규칙
#plot_ly(데이터프레임, x = ~시간, y = ~값)
plot_ly(sdot, x = ~hour, y = ~dust, name = 'trace 0', type = 'scatter', mode = 'markers',
marker = list(opacity = 0.4, color = "blue"))
plot_ly(sdot, x = ~hour, y = ~super_dust, name = 'trace 0', type = 'scatter', mode = 'markers',
marker = list(opacity = 0.4, color = "red"))
#센서 설치정보 불러오기
model <- read.csv("data/model.csv", head = TRUE, fileEncoding = 'euc-kr')
#SDot 데이터와 설치 정보 결합하기
sdot_model <- merge(sdot, model, by ="model", all.x = TRUE)
#경도 위도 이름 변경 및 사용데이터 결합
sdot_model <- sdot_model %>% dplyr::rename(long = y, lat = x)
sdot_model <- sdot_model %>% group_by(gover, model, hour) %>% summarise(long = mean(long), lat = mean(lat), super_dust = mean(super_dust), dust = mean(dust))
## `summarise()` regrouping output by 'gover', 'model' (override with `.groups` argument)
지도정보 불러오기 :
#서울시/자치구/이름 파일
p<- read.any("data/sample.csv", header = TRUE) #시각화할 데이터셋
#대한민국 시군구 행정구역 shp파일
map =readOGR('data/TL_SCCO_SIG.shp',encoding = 'cp949')
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/gim-ilhong/Documents/mac/Github_S-Dot/S_DoT/data/TL_SCCO_SIG.shp", layer: "TL_SCCO_SIG"
## with 250 features
## It has 3 fields
#행정구역 shp 전처리
map <- spTransform(map, CRSobj = CRS('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'))
map@polygons[[1]]@Polygons[[1]]@coords %>% head(n = 10L)
## [,1] [,2]
## [1,] 127.0086 37.58047
## [2,] 127.0087 37.58045
## [3,] 127.0088 37.58044
## [4,] 127.0089 37.58042
## [5,] 127.0091 37.58039
## [6,] 127.0092 37.58039
## [7,] 127.0092 37.58038
## [8,] 127.0093 37.58038
## [9,] 127.0093 37.58038
## [10,] 127.0094 37.58036
new_map <- fortify(map, region = 'SIG_CD')
## SpP is invalid
new_map$id <- as.numeric(new_map$id)
#대한민국 시군구 행정구역 중 서울시만 불러오기
seoul_map <- new_map[new_map$id <= 11740,]
#서울시 자치구 이름 표기 정보 결합
P_merge <- merge(seoul_map, p, by='id')
P_merge <- P_merge %>% dplyr::rename(gover = 시군구명)
top_stat_sdot<-sdot_model %>% group_by(gover, hour) %>% summarise(dust = mean(dust), super_dust = mean(super_dust))
## `summarise()` regrouping output by 'gover' (override with `.groups` argument)
top_merge<-merge(P_merge, top_stat_sdot, by="gover", all.x =TRUE)
################### 아주 중요 ##############################
split_data_poly = lapply(unique(P_merge$group), function(x) {
df = as.matrix(P_merge[P_merge$group == x, c("long", "lat")])
polys = Polygons(list(Polygon(df)), ID = x)
return(polys)
})
#split_data_poly
data_polys = SpatialPolygons(split_data_poly)
#지도에 SDOT 설치 정보 뿌리기
#테스트버전
leaflet(data_polys) %>%
setView(lng=126.9784, lat=37.566, zoom=11) %>%
#자치구 경계선
addPolygons(fillColor = "white",weight ="2",color = "black", opacity = 0.8 ) %>%
#맵 스타일
addProviderTiles('CartoDB.Positron') %>%
#sdot_model에 담겨있는 위치정보를 맵에 보여주기
addCircleMarkers(data = sdot_model, lng=~long, lat=~lat, color="#20639B", radius = "2",stroke = TRUE, fillOpacity = 0.8, weight =1)
# 미세먼지 값 표시하기
# 미세먼지 범례설정
sdot_model$dust_range <- cut(sdot_model$dust,
c(0,30,80,150,999), include.lowest = T,
labels = c('0','15','35','75'))
colors_sdot <- c("#3982BA", "#66B74F", "#FAE284", "#E93A30")
dust_color_sdot <- colorFactor(palette = colors_sdot , sdot_model$dust_range)
# 초미세먼지 범례설정
sdot_model$super_dust_range <- cut(sdot_model$super_dust,
c(0,15,35,75,999), include.lowest = T,
labels = c('0','15','35','75'))
colors_sdot <- c("#3982BA", "#66B74F", "#FAE284", "#E93A30")
super_dust_color_sdot <- colorFactor(palette = colors_sdot , sdot_model$super_dust_range)
#범례 커스텀 옵션
addLegendCustom <- function(map, position, colors, labels, sizes, opacity = 0.5, title){
colorAdditions <- paste0(colors, "; border-radius: 50%; width:", sizes, "px; height:", sizes, "px")
labelAdditions <- paste0("<div style='display: inline-block;height: ",
sizes, "px;margin-top: 4px;line-height: ", sizes, "px;'>",
labels, "</div>")
return(addLegend(map, position = "bottomright",
colors = colorAdditions,
labels = labelAdditions, opacity = opacity, title = title))
}
# 일별
mean_sdot_model <- sdot_model %>% group_by(model) %>% summarise(super_dust =mean(super_dust), dust = mean(dust), long = mean(long), lat = mean(lat))
## `summarise()` ungrouping output (override with `.groups` argument)
# 일별 미세먼지 범례설정
mean_sdot_model$dust_range <- cut(mean_sdot_model$dust,
c(0,30,80,150,999), include.lowest = T,
labels = c('0','15','35','75'))
colors_sdot <- c("#3982BA", "#66B74F", "#FAE284", "#E93A30")
dust_color_sdot <- colorFactor(palette = colors_sdot , mean_sdot_model$dust_range)
# 일별 초미세먼지 범례설정
mean_sdot_model$super_dust_range <- cut(mean_sdot_model$super_dust,
c(0,15,35,75,999), include.lowest = T,
labels = c('0','15','35','75'))
colors_sdot <- c("#3982BA", "#66B74F", "#FAE284", "#E93A30")
super_dust_color_sdot <- colorFactor(palette = colors_sdot , mean_sdot_model$super_dust_range)
# 미세먼지 농도 지도에 표기
leaflet(data_polys) %>%
setView(lng=126.9784, lat=37.566, zoom=11) %>%
addPolygons(fillColor = "white",weight ="2",color = "black", opacity = 0.8 ) %>%
addProviderTiles('CartoDB.Positron') %>%
addCircleMarkers(data = mean_sdot_model, lat = ~lat, lng = ~long,
color = ~dust_color_sdot(dust_range), popup = sdot_model$dust,
radius = ~sqrt(dust/3), stroke = FALSE, fillOpacity = 0.2, ) %>%
addLegendCustom("bottomright",
colors =c("#3982BA", "#66B74F", "#FAE284", "#E93A30"),
labels= c("좋음 : 0 ~ 30㎍/㎥", "보통 : 31 ~ 80㎍/㎥","나쁨 : 81 ~ 150㎍/㎥","매우나쁨 : 151㎍/㎥ 이상"),
sizes = c(10, 12, 13,15),
opacity = 1,
title= "미세먼지(PM10) 기준 등급")